package com.gcloud.dropbox;

import com.dropbox.client2.DropboxAPI;
import com.dropbox.client2.DropboxAPI.Entry;
import com.dropbox.client2.exception.DropboxException;
import com.dropbox.client2.exception.DropboxIOException;
import com.dropbox.client2.exception.DropboxParseException;
import com.dropbox.client2.exception.DropboxPartialFileException;
import com.dropbox.client2.exception.DropboxServerException;
import com.dropbox.client2.exception.DropboxUnlinkedException;
import com.example.gcode.GCloudSingleton;
import com.utils.GCloudUtil;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

public class ListDropBoxFile extends AsyncTask<Void, Long, Boolean> {

	private Context mContext;
	private final ProgressDialog mDialog;
	private DropboxAPI<?> mApi;
	private String mPath;
	private Long mFileLen;
	private String mErrorMsg;
	GCloudSingleton gCloudSingleton;

	public ListDropBoxFile(Context context, DropboxAPI<?> api,
			String dropboxPath) {
		mContext = context.getApplicationContext();
		mApi = api;
		mPath = dropboxPath;
		mDialog = new ProgressDialog(context);
		 mDialog.setMessage("Getting information from DropBox service");
		 mDialog.show();
	}

	@Override
	protected Boolean doInBackground(Void... params) {
		try {
			// Get the metadata for a directory
			Log.i("doInBackGround", "working in background");
			Entry dirent = mApi.metadata(mPath, 2500, null, true, null);
			Log.i("doInBackGround", "after get service info");
			 if (!dirent.isDir || dirent.contents == null) {
			 // It's not a directory, or there's nothing in it
			 mErrorMsg = "File or empty directory";
			 Log.i("doInBackGround",mErrorMsg);
			 return false;
			 }

			// Make a list of everything in it that we can get a thumbnail for
			// ArrayList<Entry> thumbs = new ArrayList<Entry>();
			// gCloudSingleton.getInstance().setFilesFromAllService(GCloudUtil.dropBoxFilesToGCloudFiles(dirent));

			for (Entry ent : dirent.contents) {
				Log.i("ListDropBox", ent.fileName());
			}
			return true;

		} catch (DropboxUnlinkedException e) {
			// The AuthSession wasn't properly authenticated or user unlinked.
		} catch (DropboxPartialFileException e) {
			// We canceled the operation
			mErrorMsg = "Download canceled";
		} catch (DropboxServerException e) {
			// Server-side exception. These are examples of what could happen,
			// but we don't do anything special with them here.
			if (e.error == DropboxServerException._304_NOT_MODIFIED) {
				// won't happen since we don't pass in revision with metadata
			} else if (e.error == DropboxServerException._401_UNAUTHORIZED) {
				// Unauthorized, so we should unlink them. You may want to
				// automatically log the user out in this case.
			} else if (e.error == DropboxServerException._403_FORBIDDEN) {
				// Not allowed to access this
			} else if (e.error == DropboxServerException._404_NOT_FOUND) {
				// path not found (or if it was the thumbnail, can't be
				// thumbnailed)
			} else if (e.error == DropboxServerException._406_NOT_ACCEPTABLE) {
				// too many entries to return
			} else if (e.error == DropboxServerException._415_UNSUPPORTED_MEDIA) {
				// can't be thumbnailed
			} else if (e.error == DropboxServerException._507_INSUFFICIENT_STORAGE) {
				// user is over quota
			} else {
				// Something else
			}
			// This gets the Dropbox error, translated into the user's language
			mErrorMsg = e.body.userError;
			if (mErrorMsg == null) {
				mErrorMsg = e.body.error;
			}
		} catch (DropboxIOException e) {
			// Happens all the time, probably want to retry automatically.
			mErrorMsg = "Network error.  Try again.";
		} catch (DropboxParseException e) {
			// Probably due to Dropbox server restarting, should retry
			mErrorMsg = "Dropbox error.  Try again.";
		} catch (DropboxException e) {
			// Unknown error
			mErrorMsg = "Unknown error.  Try again.";
		}
		return false;
	}

	@Override
	protected void onProgressUpdate(Long... progress) {
		int percent = (int) (100.0 * (double) progress[0] / mFileLen + 0.5);
		mDialog.setProgress(percent);
	}

	@Override
	protected void onPostExecute(Boolean result) {
		mDialog.dismiss();
		if (result) {
			showToast("The files were listing well");
		} else {
			showToast(mErrorMsg);
		}
	}

	private void showToast(String msg) {
		Toast error = Toast.makeText(mContext, msg, Toast.LENGTH_LONG);
		error.show();
	}

}
